package com.mini.service;

import com.mini.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author shixiaopeng
 */
@Slf4j
@Service
public class SendMsg2RabbitMQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *
     * @param content 消息内容
     */
    @Async("asyncMqExecutor")
    public void sendMsg(Object content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_DATA, MqConstants.ROUTE_KEY_SITE, content, a ->{
            a.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
            return a;
        },correlationId);
    }

}
